package com.top75;

import com.bean.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: huangzhigao
 * @Date: 2023/5/22 20:39
 */
public class Code872 {
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        List<Integer> res1 = new ArrayList<>();
        List<Integer> res2 = new ArrayList<>();
        getLeaf(root1, res1);
        getLeaf(root2, res2);
        return res1.equals(res2);
    }

    public void getLeaf(TreeNode root, List<Integer> res) {
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            res.add(root.val);
            return;
        }
        getLeaf(root.left, res);
        getLeaf(root.right, res);
    }
}
